package com.tanhua.appserver.controller;

import com.tanhua.appserver.hanlder.UserHolder;
import com.tanhua.appserver.service.BlackListService;
import com.tanhua.appserver.service.QuestionService;
import com.tanhua.appserver.service.SettingsService;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.CheckCodeResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private BlackListService blackListService;
    /**
     * 查看通用设置时，将数据库中的所有设置回显
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity getSettings(){
        Long userId = UserHolder.getUserId();
        String phone = UserHolder.getMobile();
        SettingsVo settingsVo =settingsService.getSettings(userId,phone);

        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 更新陌生人问题
     * @param map
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map map){
        String question = (String) map.get("content");
        Long userId = UserHolder.getUserId();
        questionService.update(question,userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新通知设置
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateSetting(@RequestBody Map map){
        Long userId = UserHolder.getUserId();
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        Settings settings=settingsService.findSettingByUserId(userId);
        settings.setPinglunNotification(pinglunNotification);
        settings.setLikeNotification(likeNotification);
        settings.setGonggaoNotification(gonggaoNotification);
        settingsService.update(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int size){
        PageResult pr = blackListService.page(page, size);
        return ResponseEntity.ok(pr);
    }
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable long uid){
        settingsService.deleteBlacklist(uid);
        return ResponseEntity.ok(null);
    }

    /**
     *
     * 修改手机号发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //发送验证码
        settingsService.sendVerificationCode();

        return ResponseEntity.ok(null);
    }
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String code = (String) map.get("verificationCode");
        //调用settingsService中的findUserById查询要修改的电话是否存在如果存在，抛出异常
        boolean verification=settingsService.checkVerificationCode(code);
        CheckCodeResult checkCodeResult = new CheckCodeResult(verification);
        return ResponseEntity.ok(checkCodeResult);
    }

    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        settingsService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }
}
